package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcCart;
import com.ruoyi.fun.service.IEcCartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 购物车Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/fun/cart")
public class EcCartController extends BaseController
{
    @Autowired
    private IEcCartService ecCartService;

    /**
     * 查询购物车列表
     */
    @PreAuthorize("@ss.hasPermi('fun:cart:list')")
    @DataScope(userAlias = "c", permission = "fun:cart:list")
    @GetMapping("/list")
    public TableDataInfo list(EcCart ecCart)
    {
        startPage();
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecCart.setOrgId(orgId);
        List<EcCart> list = ecCartService.selectEcCartList(ecCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @PreAuthorize("@ss.hasPermi('fun:cart:export')")
    @DataScope(userAlias = "c", permission = "fun:cart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcCart ecCart)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecCart.setOrgId(orgId);
        List<EcCart> list = ecCartService.selectEcCartList(ecCart);
        ExcelUtil<EcCart> util = new ExcelUtil<EcCart>(EcCart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:cart:query')")
    @GetMapping(value = "/{cartId}")
    public AjaxResult getInfo(@PathVariable("cartId") Long cartId)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        return success(ecCartService.selectEcCartByCartIdAndOrgId(cartId, orgId));
    }

    /**
     * 新增购物车
     */
    @PreAuthorize("@ss.hasPermi('fun:cart:add')")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcCart ecCart)
    {
        ecCart.setOrgId(getOrgId());
        return toAjax(ecCartService.insertEcCart(ecCart));
    }

    /**
     * 修改购物车
     */
    @PreAuthorize("@ss.hasPermi('fun:cart:edit')")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcCart ecCart)
    {
        return toAjax(ecCartService.updateEcCart(ecCart));
    }

    /**
     * 删除购物车
     */
    @PreAuthorize("@ss.hasPermi('fun:cart:remove')")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cartIds}")
    public AjaxResult remove(@PathVariable Long[] cartIds)
    {
        return toAjax(ecCartService.deleteEcCartByCartIds(cartIds));
    }
    /**
     * 根据cartId和组织ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:eccart:query')")
    @GetMapping(value = "/{cartId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("cartId") Long cartId, @PathVariable("orgId") Long orgId)
    {
        return success(ecCartService.selectEcCartByCartIdAndOrgId(cartId, orgId));
    }

    /**
     * 根据组织ID查询列表
     */
    @PreAuthorize("@ss.hasPermi('fun:eccart:list')")
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        EcCart ecCart = new EcCart();
        List<EcCart> list = ecCartService.selectEcCartListByOrgId(ecCart, orgId);
        return getDataTable(list);
    }


}